//////////////////////////////////////////////////////////////////////////////////
//
//	Setinfo
//		- Version 1.0
//		- 04.18.2008
//		- diamond-optic
//
//////////////////////////////////////////////////////////////////////////////////
//
// Credits:
//	- NHDriver4 for his tagline plugin as a guide to writing/reading files
//	- sawce for the 'access_wrap' stock
//
// Information:
//
// 	- Use !setinfo in your server, just like in IRC
//
//	- Your setinfo line will be displayed to the rest
//	  of the server when you connect...
//
// CVARs: 
//
//	amx_setinfo "1" 		//turn ON(1)/OFF(0)
//	amx_setinfo_delay "1.0"		//delay between joining and printing msg (1.0 = 1 second)
//
// Chat Commands:
//
//	!setinfo		//Will show you your current setinfo line
//	!setinfo yada yada	//Will set your setinfo line (replace yada yada w/ your text)
//	!delinfo		//Will delete your current setinfo line
//
// Extra:
//
//	Place the "setinfo.ini" file in your amxmodx/configs/ directory
//
// Changelog:
//
//	- 12.09.2006 Version 1.0
//		Initial Release
//
//	- 04.18.2008 Version 1.0 (re-release)
//		Updated public cvar format
//
//////////////////////////////////////////////////////////////////////////////////

#include <amxmodx>
#include <amxmisc>

//////////////////////////////////////////////////////////////////////////////
#define SETINFO_LEVEL	ADMIN_ALL	//level needed to use setinfo lines
//////////////////////////////////////////////////////////////////////////////

#define VERSION "1.0"
#define SVERSION "v1.0 - by diamond-optic (www.AvaMods.com)"

new p_setinfo, p_delay
new filename[512]

public plugin_init()
{
	register_plugin("Setinfo", VERSION, "diamond-optic")
	register_cvar("amx_setinfo_stats",SVERSION,FCVAR_SERVER|FCVAR_SPONLY)

	p_setinfo = register_cvar("amx_setinfo","1")		//turn on/off
	p_delay = register_cvar("amx_setinfo_delay","1.0")	//delay before printing
	
	//chat command
	register_clcmd("say","setSetInfo",0,"Setinfo Commands")
	register_clcmd("say_team","setSetInfo",0,"Setinfo Commands")
}

public plugin_cfg()
{
	new configdir[64]
	get_configsdir(configdir, 63)
	format(filename, 511, "%s/setinfo.ini",configdir)	
}

public client_putinserver(id) 
	if(is_user_connected(id) && get_pcvar_num(p_setinfo) && access_wrap(id,SETINFO_LEVEL))
		set_task(get_pcvar_float(p_delay), "connectSetInfo", id)

public connectSetInfo(id)
{
	if(is_user_connected(id) && get_pcvar_num(p_setinfo) && access_wrap(id,SETINFO_LEVEL))
		{			
		if(file_exists(filename)) 
			{			
			new authid[64]
			get_user_authid(id, authid, 63)
				
			new line, text[512], txtlen
				
			while((read_file(filename, line++, text, 511, txtlen)) > 0 ) 
				{
				if(text[0] == ';' || (text[0] == '/' && text[1] == '/'))
					continue
				
				new authid2[64], setinfo[255]
				parse(text, authid2, 63, setinfo, 254)
				
				if(equali(authid, authid2))
					{
					new name[33]
					get_user_name(id, name, 32)
					
					if(strlen(setinfo) > 0)
						client_print(0, print_chat, "<<%s>> %s",name,setinfo)
						
					return PLUGIN_CONTINUE
					}
				}
			}
		else
			log_to_file("setinfo.log","FILE ERROR - ^"%s^"",filename)
	}
	return PLUGIN_HANDLED
}

public setSetInfo(id, level, cid) 
{		
	if(is_user_connected(id) && get_pcvar_num(p_setinfo) && access_wrap(id, SETINFO_LEVEL))
		{	
		new arg[9]
		read_argv(1,arg,8)
		
		if(equali(arg,"!delinfo"))
			deleteOwnSetInfo(id)
			
		else if(equali(arg,"!setinfo"))
			{		
			new tag[256]
			read_args(tag,255)
			remove_quotes(tag)
			
			if(equali(tag,"!setinfo"))
				showOwnSetInfo(id)
			else
				{
				replace(tag,255,"!setinfo ","")
					
				if(file_exists(filename)) 
					{
					new authid[64],fileLine
					get_user_authid(id, authid, 63)
									
					new line,text[512],txtlen
					
					while((fileLine = read_file(filename, line++, text, 511, txtlen)) > 0) 
						{
						if(text[0] == ';' || (text[0] == '/' && text[1] == '/'))
							continue
						
						new authid2[64],setinfo[255]
						parse(text,authid2,63,setinfo,254)
										
						if(equali(authid,authid2))
							{						
							if(strlen(setinfo) > 0)
								{
								new savetext[512]
								format(savetext, 511, "%s ^"%s^"", authid, tag)
								write_file(filename, savetext, fileLine-1)
									
								client_print(id,print_chat,"** Your setinfo line has been replaced (%s)",authid)
								log_to_file("setinfo.log","OVERWROTE SETINFO: ^"%s^" - ^"%s^"",authid,savetext)
								}
								
							return PLUGIN_CONTINUE
							}
						}
					if(fileLine == 0)
						{
						if(strlen(tag) == 0)
							{
							client_print(id,print_chat,"** Your initial setinfo line cannot be blank (%s)",authid)
							
							return PLUGIN_HANDLED
							}
						new savetext[512]
						format(savetext, 511, "%s ^"%s^"",authid, tag)
						write_file(filename, savetext, -1)
						client_print(id,print_chat,"** Your setinfo line has been saved (%s)",authid)
						
						log_to_file("setinfo.log","SAVED NEW SETINFO: ^"%s^" - ^"%s^"",authid,savetext)
						
						return PLUGIN_CONTINUE
						}
					}
				else
					log_to_file("setinfo.log","FILE ERROR - ^"%s^"",filename)
				}
			}
		}
		
	return PLUGIN_CONTINUE
}

public showOwnSetInfo(id)
{
	if(is_user_connected(id) && get_pcvar_num(p_setinfo) && access_wrap(id,SETINFO_LEVEL))
		{			
		if(file_exists(filename)) 
			{			
			new authid[64]
			get_user_authid(id,authid,63)
				
			new line,text[512],txtlen
				
			while((read_file(filename,line++,text,511,txtlen)) > 0 ) 
				{
				if(text[0] == ';' || (text[0] == '/' && text[1] == '/'))
					continue
				
				new authid2[64], setinfo[255]
				parse(text,authid2,63,setinfo,254)
				
				if(equali(authid,authid2))
					{				
					if(strlen(setinfo) > 0)
						client_print(id,print_chat,"* Your setinfo: %s",setinfo)
						
					return PLUGIN_CONTINUE
					}
				}
			client_print(id,print_chat,"* You have yet to set your setinfo line! *")		
			}
		else
			log_to_file("setinfo.log","FILE ERROR - ^"%s^"",filename)
	}
	
	return PLUGIN_CONTINUE	
}

public deleteOwnSetInfo(id)
{
	if(is_user_connected(id) && get_pcvar_num(p_setinfo) && access_wrap(id,SETINFO_LEVEL))
		{			
		if(file_exists(filename)) 
			{			
			new authid[64],fileLine
			get_user_authid(id,authid,63)
				
			new line,text[512],txtlen
				
			while((fileLine = read_file(filename, line++, text, 511, txtlen)) > 0) 
				{
				if(text[0] == ';' || (text[0] == '/' && text[1] == '/'))
					continue
				
				new authid2[64], setinfo[255]
				parse(text,authid2,63,setinfo,254)
				
				if(equali(authid,authid2))
					{				
					new savetext[1]
					format(savetext, 0, "")
					write_file(filename, savetext, fileLine-1)
									
					client_print(id,print_chat,"** Your setinfo line has been deleted (%s)",authid)
					log_to_file("setinfo.log","DELETED SETINFO: ^"%s^" - ^"%s^"",authid,savetext)

					return PLUGIN_CONTINUE
					}
				}
			client_print(id,print_chat,"* You dont have a setinfo line to delete! *")		
			}
		else
			log_to_file("setinfo.log","FILE ERROR - ^"%s^"",filename)
	}
	
	return PLUGIN_CONTINUE	
}

stock access_wrap(id,level)
{
	if(!level) return 1;
	
	return access(id,level)
}
